<template>
  <div>
    <!-- 回复 -->
    <div class="footer" v-if="!show">
      <input type="text" class="left" @focus="isFocus" placeholder="写跟帖" />
      <div class="right">
        <div class="comments">
          <span class="iconfont iconpinglun-"></span>
          <span class="text">102</span>
        </div>
        <div class="collection">
          <span class="iconfont iconshoucang"></span>
        </div>
        <div class="share">
          <span class="iconfont iconfenxiang"></span>
        </div>
      </div>
    </div>
    <div class="deactive" v-if="show">
      <textarea
        ref="textarea"
        rows="3"
        placeholder="写评论"
        @blur="hideText"
        v-model="content"
      ></textarea>
      <button @click="send()">发送</button>
    </div>
  </div>
</template>

<script>
import { publicComment } from "@/api/myComment";
import eventBus from "../utils/eventBus";
export default {
  data() {
    return {
      show: false,
      content: "",
      parent_id: "",
    };
  },
  mounted() {
    eventBus.$on("sendMsg", (parentId) => {
      this.parent_id = parentId;
      this.isFocus();
    });
  },
  methods: {
    isFocus() {
      this.show = true;
      this.$nextTick(() => {
        this.$refs.textarea.focus();
      });
    },
    hideText() {
      setTimeout(() => {
        this.show = false;
        this.content = "";
        this.parent_id = "";
      }, 100);
    },
    send() {
      // console.log(this.$route.params.id);
      let id = this.$route.params.id;
      let data = {
        content: this.content,
        parent_id: this.parent_id,
      };
      // console.log(data);
      publicComment(id, data).then((res) => {
        console.log(res);
        if (res.data.message == "评论发布成功") {
          this.$emit("reloadComment");
          this.content = "";
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.footer {
  padding: 10 /360 * 100vw 15 /360 * 100vw;
  position: fixed;
  display: flex;
  justify-content: space-between;
  align-items: center;
  bottom: 0;
  width: 100%;
  // height: 60px;
  background: #f2f2f2;
  .left {
    padding-left: 20 /360 * 100vw;
    width: 200 /360 * 100vw;
    height: 30 /360 * 100vw;
    background-color: #d7d7d7;
    border-radius: 15 /360 * 100vw;
  }
  .right {
    width: 100 /360 * 100vw;
    display: flex;
    justify-content: space-between;
    align-items: center;
    span {
      font-size: 20 /360 * 100vw;
    }
    .comments {
      position: relative;
      .text {
        position: absolute;
        top: -4px;
        left: 7px;
        font-size: 10 /360 * 100vw;
        background-color: red;
        padding: 0 5 /360 * 100vw;
        border-radius: 8 /360 * 100vw;
        color: #fff;
      }
    }
  }
}
.deactive {
  padding: 10 /360 * 100vw 15 /360 * 100vw;
  position: fixed;
  display: flex;
  align-items: flex-end;
  bottom: 0;
  width: 100%;
  background: #f2f2f2;
  textarea {
    padding: 10 /360 * 100vw;
    width: 250 /360 * 100vw;
    border: 0;
    background-color: #d7d7d7;
    border-radius: 15 /360 * 100vw;
  }
  button {
    margin-left: 10 /360 * 100vw;
    background-color: red;
    padding: 5 /360 * 100vw 20 /360 * 100vw;
    border-radius: 20 /360 * 100vw;
    color: #fff;
  }
}
</style>